<?php

namespace Biz\service\wiki;

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\pockets\serviceTrait\_Async;
use Biz\service\Goods as serviceGoods;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\cache\BlackList;
use Doraemon\service\Photos as sPhotos;
use Doraemon\tools\HhzClient;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log;

class RelatePhoto
{
    use _Async;

    const STATUS_PRIVATE = 11;

    /**
     * 图片关联的wiki和商品信息
     * @param $noteIds
     * @return mixed
     */
    public static function getWikiListByPhotoIds(array $noteIds, $fieldsGroup = Wiki::FIELDS_GROUP_LIST)
    {
        $result = [];
        foreach ($noteIds as $noteId) {
            $result[$noteId]['list'] = [];
        }
        $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
        $aRet = $wikiGrpcClient->getRelatedWikiListByObjIds([
            "objIds" => $noteIds,
            "objType" => mObjRelateWiki::OBJ_TYPE_NOTE,
            "fieldsGroup" => $fieldsGroup,
            "uid" => HhzClient::getUid()
        ]);
        if ($aRet['biz_code'] != 0) {
            return [];
        }
        $res = $aRet['result']['list'] ?? [];
        foreach ($noteIds as $noteId) {
            $relInfo = $res[$noteId] ?? [];
            if ($relInfo) {
                $result[$noteId]['list'] = $relInfo['wiki_list'];
            } else {
                $result[$noteId]['list'] = [];
            }
        }

        return $result;
    }

    /**
     * 查询note关联的wiki的图片
     * @param array $noteIds
     * @return array
     */
    public static function getRelateWikiImgByNoteIds(array $noteIds)
    {
        $result = [];
        foreach ($noteIds as $noteId) {
            $result[$noteId] = [];
        }
        $wikiGrpcClient = \Wiki\GrpcClient::getInstance();
        $aRet = $wikiGrpcClient->getRelatedWikiListByObjIds([
            "objIds" => $noteIds,
            "objType" => mObjRelateWiki::OBJ_TYPE_NOTE,
            "fieldsGroup" => Wiki::FIELDS_GROUP_SIMPLE_LIST,
            "uid" => HhzClient::getUid()
        ]);
        if ($aRet['biz_code'] != 0) {
            return [];
        }
        $res = $aRet['result']['list'] ?? [];
        foreach ($res as $noteId => $row) {
            foreach ($row['wiki_list'] as $wikiInfo) {
                $result[$noteId][] = $wikiInfo['cover_img'];
            }
        }
        return $result;
    }


    /**
     * 图片关联wiki商品（点图标签适用）
     * @param $noteId
     * @param $picId
     * @param array $wikiIds
     * @param $uid
     * @return array
     * @throws \Exception
     */
    public static function photoRelateWikiTag($noteId, $picId, array $wikiIds, $uid)
    {
        try {
            if (!$uid || !$noteId || !$picId) {
                throw new \Exception('参数错误');
            }
            return ObjRelateWiki::noteRelateWiki($uid, $noteId, $wikiIds, $picId, mObjRelateWiki::REL_OBJ_TYPE_PIC, mObjRelateWiki::TYPE_USER_TAG);
        } catch (Exception $e) {
            Log::wiki_relatePhoto_warning('图片关联wiki标签失败#data:' . json_encode(['note_id' => $noteId, 'pic_id' => $picId, 'wiki_ids' => $wikiIds, 'uid' => $uid]));
            throw $e;
        }
    }

    /**
     * note关联wiki商品
     * @param $noteId
     * @param array $wikiIds
     * @param $uid
     * @return array
     * @throws \Exception
     */
    public static function noteRelateWiki($noteId, array $wikiIds, $uid, $type = mObjRelateWiki::TYPE_USER_REL)
    {
        try {
            if (!$uid || !$noteId) {
                throw new \Exception('参数错误');
            }
            return ObjRelateWiki::noteRelateWiki($uid, $noteId, $wikiIds, '', 0, $type);
        } catch (Exception $e) {
            Log::wiki_relateNote_warning('note关联wiki失败#data:' . json_encode(['note_id' => $noteId, 'wiki_ids' => $wikiIds, 'uid' => $uid, 'type' => $type]));
            throw $e;
        }
    }
}
